package sandbox.vaadin.skiller.skillgroupmanager;

import org.apache.log4j.Logger;

import sandbox.vaadin.Util;
import sandbox.vaadin.skiller.entity.Skill;
import sandbox.vaadin.skiller.entity.SkillEditor;

import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class SkillTableView extends VerticalLayout
{
  private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(SkillTableView.class);

	private SkillgroupManagerContext context;

	public SkillTableView(SkillgroupManagerContext context)
	{
		super();
		this.context = context;
		init();
	}

	public SkillTableView(SkillgroupManagerContext context, Util.Size size)
	{
		this(context);
		Util.setSize(this, size);		
	}

	public SkillTableView(
			SkillgroupManagerContext context, Util.Size width, Util.Size height)
  {
		this(context);		
		Util.setSize(this, width, height);
  }

	private void init()
	{
		addComponent(newSkillsTable());
		addComponent(newSkillsButtonBar());
	}

	private Component newSkillsButtonBar()
  {
		HorizontalLayout result = new HorizontalLayout();

		result.addComponent(newButtonEditSkill());

		return result;
  }

	private Component newSkillsTable()
  {
		Table result = new Table("skills", context.getSkillContainer());

//		result.setItemCaptionMode(ItemCaptionMode.PROPERTY);
//		result.setItemCaptionPropertyId("name");
		result.setVisibleColumns(new Object[] { "name" });
		result.setSelectable(true);
		result.setImmediate(true);
		result.addValueChangeListener(new SkillValueChangeListener());

		return result;
  }

	private class SkillValueChangeListener implements ValueChangeListener
	{
		private static final long serialVersionUID = 1L;

		@Override
		public void valueChange(ValueChangeEvent event)
		{
			Object value = event.getProperty().getValue();

			LOGGER.debug("event.property.value: " + value);
			LOGGER.debug("event.property.type: " + event.getProperty().getType());

			if (value != null)
			{
				context.setSkillSelectedEntityItem(
						context.getSkillContainer().getItem(value));
				LOGGER.debug("entity item: " + context.getSkillSelectedEntityItem());
				LOGGER.debug("entity: " + getSkillSelected());
			}
			else
			{
				// happens if user clicks on already selected node
			}
		}
	}
	
	private Skill getSkillSelected()
	{
		EntityItem<Skill> skillSelectedEntityItem =
				context.getSkillSelectedEntityItem();

		if (skillSelectedEntityItem != null)
		{
			return skillSelectedEntityItem.getEntity();
		}

		return null;
	}

	private Component newButtonEditSkill()
	{
		Button result = new Button("edit");

		result.addClickListener(new ButtonEditSkillListener());
		
		return result ;
	}

	private class ButtonEditSkillListener implements Button.ClickListener
	{
		private static final long serialVersionUID = 1L;

		@Override
    public void buttonClick(ClickEvent event)
    {
			LOGGER.debug("edit button clicked");
			
			Skill skillSelected = getSkillSelected();
			
			if (skillSelected != null)
			{
				LOGGER.debug("selected skill: " + skillSelected);
				UI.getCurrent().addWindow(
						new SkillEditor(context.getSkillSelectedEntityItem())); 
			}
    }
	}
}